package com.controller.sys.user.login;

import com.model.base.Result;
import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.core.email.EmailConfig;
import com.core.email.EmailService;
import com.model.dto.sys.LoginController.GetCodeByEmailDTO;
import com.model.vo.sys.code.GetCodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Author: zql
 * @Description: 邮箱服务
 * @Data: 8:35 2021/7/22
 */
@Api(tags = {"公共"})
@Slf4j
@RestController
@RequestMapping("/v1")
public class EmailController {

    @Resource
    EmailService emailService;

    /**
     * @Author: zql
     * @Description: 获取邮箱号验证码服务 同一个邮箱30s一次
     * @param getCodeByEmailDTO 邮箱号
     * @Data: 10:41 2021/5/12
     */
    @ApiOperation(value = "获取邮箱验证码服务")
    @Log(title = "获取邮箱验证码服务", businessType = BusinessType.GRANT)
    @PostMapping("/email/getCode")
    public Result<GetCodeVO> getCode(@RequestBody @Valid GetCodeByEmailDTO getCodeByEmailDTO) {
        long countDown = emailService.countDown(getCodeByEmailDTO.getEmail());
        GetCodeVO getCodeVO = new GetCodeVO();
        getCodeVO.setCountDown(countDown);
        if (getCodeVO.getCountDown() <= 0) {
            getCodeVO.setCountDown((long) EmailConfig.getInterval());
            log.info("短信验证请求，邮箱:{}", getCodeByEmailDTO.getEmail());
            emailService.sendCode(getCodeByEmailDTO.getEmail());
            return Result.suc(getCodeVO);
        }
        log.warn("邮箱验证，邮箱号{}，距离下次发送剩余时间：{}",getCodeByEmailDTO.getEmail(), countDown);
        return Result.suc();
    }
}
